package com.bins.cloud.stream.test;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

public class HexConverter {

    public static void main(String[] args) throws Exception {
//        String url = "http://localhost:9200/auth/current/user";
//        String token = "897ca1a2-41c9-4e6c-98ba-468ad1f93684";
//        Map<String, String> params = new HashMap<>();
//        apiConcurrent(url, params, token);

        String str = "你好，世界！";

        try {
            // byte[] byteArray = str.getBytes("GBK");
            // byte[] byteArray = {-60, -29, -70, -61, -93, -84, -54, -64, -67, -25, -93, -95};
            // byte[] byteArray = {-5, -124, 41, -19, 33, 25, 80, -68, 42, 106, -63};
            byte[] byteArray = {58, 17, 36, -27, -76, 76, 74, 106};
            // byte[] byteArray = {58, 17, 36, -27};
            System.out.println("转换后的byte数组：" + byteArray);
            String str2 = new String(byteArray);
            System.out.println(str2);


            byte[] lowerByteArray = str.getBytes();
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);
            byte[] buffer = new byte[1024];
            int len = inputStream.read(buffer);
            String data = new String(buffer, 0, len);
            System.out.println("Received data: " + data);

//            byte[] a = hexStringToByteArray(str);
//            for (byte b : a) {
//                System.out.print(b);
//                System.out.print(" ");
//            }

            String hex = stringToHex(str);
            System.out.println("Hexadecimal representation: " + hex);
            byte[] a = hexStringToByteArray(hex);
            for (byte b : a) {
                System.out.print(b);
                System.out.print(" ");
            }

            String input1 = "Hello, World!";
            String hex1 = stringToHex2(input1);
            System.out.println("Hexadecimal representation: " + hex1);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static String stringToHex(String input) {
        byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    public static String stringToHex2(String input) {
        byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    private static byte[] hexStringToByteArray(String s) {
        String[] hexParts = s.split(" ");
        byte[] data = new byte[hexParts.length];

        for (int i = 0; i < hexParts.length; i++) {
            data[i] = (byte) Integer.parseInt(hexParts[i], 16);
        }

        return data;
    }
}